package com.leyou.user.service;

import com.leyou.common.exception.LyException;
import com.leyou.common.exception.enums.ExceptionEnum;
import com.leyou.common.utils.BeanHelper;
import com.leyou.user.dto.InformationDTO;
import com.leyou.user.dto.LoDTO;
import com.leyou.user.entity.Areas;
import com.leyou.user.entity.Citie;
import com.leyou.user.entity.Information;
import com.leyou.user.entity.Province;
import com.leyou.user.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author LT
 * @version 1.0.0
 * @ClassName InformationService.java
 * @Description TODO
 * @createTime 2020年05月29日 22:09:00
 */

@Service
public class InformationService {

    @Autowired
    private InformationMapper informationMapper ;

    @Autowired
    private ProvinceMapper provinceMapper;

    @Autowired
    private CitieMapper citieMapper ;

    @Autowired
    private AreasMapper areasMapper ;

    @Autowired
    private UserService userService ;

    /**
     * 更新个人数据
     * @param informationDTO
     * @throws ParseException
     */
    public void update(InformationDTO informationDTO) {
        String birthday = informationDTO.getYear()+"-"+informationDTO.getMonth()+"-"+informationDTO.getDay();
        Date parse = null;
        try {
            parse = new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String location = informationDTO.getProvince()+"-"+informationDTO.getCity()+"-"+informationDTO.getDistrict();
        Information information = BeanHelper.copyProperties(informationDTO, Information.class);
        information.setBirthday(parse);
        information.setLocation(location);

        int i = informationMapper.updateByPrimaryKey(information);

        if (i!=1){
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }

    /**
     * 根据用户Id查询个人信息
     * @return
     */
    public InformationDTO selectInfoByUserId() {
        Long id = userService.selectUser();
        Information information = new Information();
        information.setUserId(id);
        List<Information> select = informationMapper.select(information);
        Information info = select.get(0);
        InformationDTO informationDTO = BeanHelper.copyProperties(info, InformationDTO.class);
        if (info.getBirthday()!=null){
            String format = new SimpleDateFormat("yyyy-MM-dd").format(info.getBirthday());
            String[] split = format.split("-");
            informationDTO.setYear(split[0]);
            informationDTO.setMonth(split[1].replaceFirst("0",""));
            informationDTO.setDay(split[2].replaceFirst("0",""));
        }
        if (info.getLocation()!=null){
            String[] split = info.getLocation().split("-");
            informationDTO.setProvince(split[0]);
            informationDTO.setCity(split[1]);
            informationDTO.setDistrict(split[2]);
        }
        return informationDTO ;
    }

    /**
     * 省市区数据联动
     * @param id
     * @return
     */
    public List<LoDTO> findAllById(String id) {
        //定义返回的数据集合
        List<LoDTO> loDTOS = new ArrayList<>();
        //如果为0，查询省表
        if (id.equals("0")){
            List<Province> provinces = provinceMapper.selectAll();
            for (Province province : provinces) {
                LoDTO loDTO = new LoDTO();
                loDTO.setId(province.getProvinceid());
                loDTO.setName(province.getProvince());
                loDTOS.add(loDTO);
            }
            return loDTOS;
        }else{
            //否则查询城市表
            Citie citie = new Citie();
            citie.setProvinceid(id);
            List<Citie> citeList = citieMapper.select(citie);
            //判断查询结果是否为空
            if (citeList.size()==0){
                //空，查询区域表
                Areas area = new Areas();
                area.setCityid(id);
                List<Areas> areaList = areasMapper.select(area);
                //判断查询结果
                if (areaList.size()==0){
                    //还是空，则抛出异常
                    throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
                }
                //不为空，封装返回
                for (Areas areas : areaList) {
                    LoDTO loDTO = new LoDTO();
                    loDTO.setId(areas.getAreaid());
                    loDTO.setName(areas.getArea());
                    loDTOS.add(loDTO);
                }
                return loDTOS;
            }
            //不为空，封装返回
            for (Citie cities : citeList) {
                LoDTO loDTO = new LoDTO();
                loDTO.setId(cities.getCityid());
                loDTO.setName(cities.getCity());
                loDTOS.add(loDTO);
            }
            return loDTOS;
        }
    }
}
